#region File information

/**
 * File: ClientPacket.cs
 * Date: 11-03-2008
 * Auth: Jan-Jaap Severs
 * 
 * Desc: A ClientPacket describes all ship and input states that need to be replicated across the network,
 *       from client to server, at an update.
 */

#endregion

#region Using statements

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.GamerServices;

#endregion

namespace Squadron.Networking
{
    /// <summary>
    /// A ClientPacket describes all ship and input states that need to be replicated across the network,
    /// from client to server, at an update.
    /// </summary>
    public class ClientPacket
    {
        #region Header information

        PacketType packetType = PacketType.PlayerInput;
        
        #endregion

        #region Input information

        UInt16 shipID;
        Vector2 yawPitchInput;
        bool fireGuns;

        #endregion
                
        public ClientPacket(Vector2 yawPitchInput, bool fireGuns, UInt16 shipID)
        {
            this.shipID = shipID;
            this.yawPitchInput = yawPitchInput;
            this.fireGuns = fireGuns;
        }

        /// <summary>
        /// Send one client's input state to the server.
        /// </summary>
        /// <param name="packetWriter"></param>
        /// <param name="sendDataOptions"></param>
        /// <param name="gamer"></param>
        /// <param name="host"></param>
        public void Write(PacketWriter packetWriter, SendDataOptions sendDataOptions, LocalNetworkGamer gamer,
            NetworkGamer host)
        {
            // Write the header information.
            packetWriter.Write((int)packetType);

            packetWriter.Write(shipID);

            // Write the input information.
            packetWriter.Write(yawPitchInput);
            packetWriter.Write(fireGuns);            

            // Send the data to the server.
            gamer.SendData(packetWriter, sendDataOptions, host);
        }

        /// <summary>
        /// Read one client's input state from the stream.
        /// </summary>
        /// <param name="gamer"></param>
        /// <param name="packetReader"></param>        
        public static ClientPacket Read(LocalNetworkGamer gamer, PacketReader packetReader)
        {
            UInt16 shipID = packetReader.ReadUInt16();

            // Read the input information.
            Vector2 yawPitchInput = packetReader.ReadVector2();
            bool fireGuns = packetReader.ReadBoolean();

            return new ClientPacket(yawPitchInput, fireGuns, shipID);
        }

        public UInt16 ID
        {
            get { return shipID; }
        }

        public Vector2 YawPitch
        {
            get { return yawPitchInput; }
        }

        public bool FireGuns
        {
            get { return fireGuns; }
        }
    }
}
